package com.tj.jinzao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.tj.jinzao.R;
import com.tj.jinzao.activity.AddressDetailActivity;
import com.tj.jinzao.adapter.AddressListItemAdapter;
import com.tj.jinzao.base.BaseDataListFragment;
import com.tj.jinzao.base.IBaseView;
import com.tj.jinzao.model.Address;
import com.tj.jinzao.presenter.AddressPresenter;
import com.tj.jinzao.widgets.EmptyView;
import com.tj.jinzao.widgets.SwipeItemLayout;

import java.util.List;

import butterknife.OnClick;

/**
 * 地址列表
 * Created by wjy on 2019/1/1.
 */
public class AddressListFragment extends BaseDataListFragment<List<Address>, AddressPresenter, Address> implements IBaseView<List<Address>> {

    public static AddressListFragment newInstance() {
        AddressListFragment fragment = new AddressListFragment();
        if (fragment != null) {
            Bundle args = new Bundle(3);
            args.putString(ARG_TAG_ID, String.valueOf(1));
            args.putInt(ARG_LOAD_ID, 1);
            args.putInt(ARG_LAYOUT_RES_ID, R.layout.fragment_address_list);
            fragment.setArguments(args);
        }
        return fragment;
    }

    @OnClick(R.id.bt_add_address)
    void addAddress() {
        startActivity(new Intent(mContext, AddressDetailActivity.class));
    }

    @Override
    public void onRequestNoData(String message) {
        setEmptyDataView();
    }

    @Override
    protected AddressPresenter createPresenter() {
        return new AddressPresenter(this);
    }

    @Override
    protected void createView(LayoutInflater inflater, View view) {
        mListView.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(mContext));
    }

    @Override
    protected RecyclerView.Adapter createAdapter() {
        AddressListItemAdapter adapter = new AddressListItemAdapter(mContext, mData);
        adapter.setListener(this);
        return adapter;
    }

    @Override
    protected void requestData() {
        mPresenter.getAddressList();
    }

    @Override
    protected void setEmptyDataView() {
        mEmptyView.setVisibility(View.VISIBLE);
        mEmptyView.setState(EmptyView.State.NO_DATA, "没有内容哦");
    }

    @Override
    protected boolean resolveData(List<Address> addresses) {
        return false;
    }

    @Override
    public void onRequestSuccess(List<Address> addresses) {
        super.onRequestSuccess(addresses);
        if (addresses != null && addresses.size() == 1 && addresses.get(0).getId() == -1) {
            refresh();
        } else {
            if (mPageNo <= 1) {
                mData.clear();
            }
            mData.addAll(addresses);
            showListView();
        }
    }

    @Override
    protected boolean canLoadMoreItems() {
        return false;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        try {
            int position = (Integer) v.getTag(R.id.position);
            Address address = mData.get(position);
            switch (v.getId()) {
                case R.id.iv_edit:
                    startActivity(new Intent(mContext, AddressDetailActivity.class)
                            .putExtra("address", address));
                    break;
                case R.id.btn_default:
                    if (!address.isIsDefault()) {
                        mPresenter.setDefaultAddress(address.getId());
                    }
                    break;
                case R.id.btn_delete:
                    mPresenter.deleteAddress(address.getId());
                    break;
                case R.id.rl_address_layout:
                    Intent intent = new Intent();
                    intent.putExtra("address",address);
                    mContext.setResult(100, intent);
                    mContext.finish();
                    break;
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData(true, false);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void itemClicked(int position, Address data) {
    }


}
